//
//  HELMyCollectCourseList.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/3/3.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELMyCollectCourseList.h"
#import "HELCourseListModel.h"
#import "HELCourseCell.h"
#import "NHCourseDetailVC.h"
@interface HELMyCollectCourseList ()

@end

@implementation HELMyCollectCourseList

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的收藏";
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64);
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"HELCourseCell" bundle:nil] forCellReuseIdentifier:@"HELCourseCell"];
    
    
    weakSelf_define;
    [self.tableView wsj_HeaderRefreshBlock:^{
        [weakSelf getMyCollectCourse:YES];
    }];
    [self.tableView wsj_FooterRefreshBlock:^{
        [weakSelf getMyCollectCourse:NO];
    }];
    [self getMyCollectCourse:YES];

}


- (void)getMyCollectCourse:(BOOL)isRecover{
    static NSUInteger page = 1;
    if (isRecover) {
        page =1;
    }else{
        page ++;
    }
    
    
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"page"] = @(page);
    params[@"rows"] = @(15);
    
  
    
    
    weakSelf_define;
    self.tableView.loading = YES;
    [[HELNetworkManager sharedMg] GET:helMyCollectCourse parameters:params success:^(id responseObject) {
        
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
        if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
            
            if (isRecover) {
                [weakSelf.dataArray removeAllObjects];
            }
            NSInteger totalPage = 0;
            if ([responseObject[@"data"][@"totalPage"] isKindOfClass:[NSNull class]]) {
                
            }else{
                totalPage =[responseObject[@"data"][@"totalPage"] integerValue];
            }
            
            NSInteger currentPage = [responseObject[@"data"][@"page"] integerValue];
            if (currentPage>=totalPage) {
                [weakSelf.tableView.footer noticeNoMoreData];
            }else{
                [weakSelf.tableView.footer resetNoMoreData];
            }
            
            [weakSelf.dataArray addObjectsFromArray:[HELCourseListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]]];
            
            [weakSelf.tableView reloadData];
            
        }
        
        
    } failure:^(NSString *err) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
    }];
    
}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  120;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HELCourseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HELCourseCell"];
    cell.courseListModel = self.dataArray[indexPath.row];
   
    cell.tag_MycourseList = YES;
   
    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HELCourseListModel *model = self.dataArray[indexPath.row];
    NHCourseDetailVC *vc =[NHCourseDetailVC new];
    vc.courseId = [model.id integerValue];
    [self.navigationController pushViewController:vc animated:YES];
    
}




@end
